package com.bbg.amyam.web.mvc.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.bbg.amyam.domain.model.School;
import com.bbg.amyam.web.mvc.exception.NoSchoolFoundException;
import com.bbg.amyam.web.util.RequestPathParameter;
import com.bbg.amyam.web.util.RequestPathPaser;
import com.bbg.amyam.web.util.menu.MenuDescriptor;
import com.bbg.amyam.web.util.menu.MenuUtil;

public class RequestPathValueInterceptor extends HandlerInterceptorAdapter {

	private static Log log = LogFactory
			.getLog(RequestPathValueInterceptor.class);

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		super.postHandle(request, response, handler, modelAndView);
		MenuDescriptor menuDescriptor = (MenuDescriptor) request
				.getAttribute("current_menuDescriptor_obj");
		School currentSchool = (School) request
				.getAttribute("current_school_obj");

		if (menuDescriptor != null && currentSchool != null) {

			if (modelAndView == null) {
				modelAndView = new ModelAndView();
			}
			modelAndView.getModel().put("menuDescriptor", menuDescriptor);
			modelAndView.getModel().put("currentSchool", currentSchool);
		}

	}

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {

		try {

			RequestPathParameter requestPathParameters = RequestPathPaser
					.parse(request);
			if (requestPathParameters != null) {
				School school = requestPathParameters.getSchool();
				String viewName = requestPathParameters.getViewName();
				String[] grantedRoles = { "" };
				if (school != null) {					
					String shortName = school.getShortName();
					if (viewName == null) {
						if ("default".equals(shortName)) {
							viewName = "public_view";
						} else {
							viewName = "school_public_view";
						}
						response.sendRedirect(request.getContextPath() + "/"
								+ shortName + "/" + viewName);
					}
					
					String currentSessionName = (String)request.getSession(true).getAttribute("_amyam_unit_name_");
					if(currentSessionName!=null && !shortName.equals(currentSessionName)){
						response.sendRedirect(request.getContextPath() + "/resources/j_spring_security_logout?_uname="
								+ shortName );
					}
					
					MenuDescriptor menuDescriptor = MenuUtil
							.getSchoolMenudescriptor(school, viewName,
									grantedRoles);

					request.setAttribute("current_menuDescriptor_obj",
							menuDescriptor);
					request.setAttribute("current_school_obj", school);
				}
			}

		} catch (NoSchoolFoundException e) {
			response.sendRedirect(request.getContextPath());
			return false;
		}

		return super.preHandle(request, response, handler);
	}

}
